package edu.neusoft.yuejian_09.model;

import java.util.List;

import edu.neusoft.yuejian_09.base.Constant;
import edu.neusoft.yuejian_09.base.RetrofitBaseModel;
import edu.neusoft.yuejian_09.base.RetrofitListener;

import edu.neusoft.yuejian_09.bean.Master;

import edu.neusoft.yuejian_09.bean.Question;
import edu.neusoft.yuejian_09.bean.Train;
import edu.neusoft.yuejian_09.service.MasterService;
import edu.neusoft.yuejian_09.service.TrainService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static edu.neusoft.yuejian_09.base.Constant.BASE_URL;

/**
 * Created by 爽 on 2017/10/9.
 */

public class MasterModel extends RetrofitBaseModel {
    private int flag=1;
    public MasterService masterService;
    public MasterModel(){
        this.masterService=retrofit.create(MasterService.class);
    }

    /*******首页中权威大师列表******/
    public void masterlist( final RetrofitListener listener){
        MasterService masterService=retrofit.create(MasterService.class);
        Call call=masterService.getAllMasters();
        Callback<List<Master>> callback=new Callback<List<Master>>(){
            @Override
            public void onResponse(Call<List<Master>> call, Response<List<Master>> response ){
                List<Master> data=response.body();
                listener.onSuccess(data,flag);//userId这里有问题
            }
            @Override
            public  void onFailure(Call<List<Master>> call,Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    //获取大师详情
    public void getMasterById(int Master_id, final RetrofitListener listener){
        Call call=masterService.getMasterById(Master_id);
        bindCallback(call,listener, Constant.MASTERBYID);
    }

    /*******大师详情中大师课堂列表，引用Train(培训)相关数据******/
    public void classlist(int Master_id, final RetrofitListener listener){
        MasterService masterService=retrofit.create(MasterService.class);
        Call call=masterService.getMasterTrains(Master_id);
        Callback<List<Train>> callback=new Callback<List<Train>>(){
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response ){
                List<Train> data=response.body();
                listener.onSuccess(data,flag);//userId这里有问题
            }
            @Override
            public  void onFailure(Call<List<Train>> call,Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    /*******大师详情中大师问答列表，引用Question(问答)相关数据******/
    public void Qalist(int Master_id, final RetrofitListener listener){
        MasterService masterService=retrofit.create(MasterService.class);
        Call call=masterService.getMasterQA(Master_id);
        Callback<List<Question>> callback=new Callback<List<Question>>(){
            @Override
            public void onResponse(Call<List<Question>> call, Response<List<Question>> response ){
                List<Question> data=response.body();
                listener.onSuccess(data,flag);//userId这里有问题
            }
            @Override
            public  void onFailure(Call<List<Question>> call,Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }


    //搜索大师
    public void getMasBySearch(String SearchKey, final RetrofitListener listener){
        MasterService masterService=retrofit.create(MasterService.class);
        Call call=masterService.getMasterBySearch(SearchKey);
        Callback<List<Master>> callback=new Callback<List<Master>>(){
            @Override
            public void onResponse(Call<List<Master>> call, Response<List<Master>> response ){
                List<Master> data=response.body();
                listener.onSuccess(data,flag);
            }
            @Override
            public  void onFailure(Call<List<Master>> call, Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    //发布问题
    public void insertNote(int user_id,int master_id,String qa_question, final RetrofitListener listener){
        MasterService masterService=retrofit.create(MasterService.class);
        Call call=masterService.insertNote(user_id,master_id,qa_question);
        Callback<Question> callback=new Callback<Question>(){
            @Override
            public void onResponse(Call<Question> call, Response<Question> response ){
                Question data=response.body();
                listener.onSuccess(data,flag);
            }
            @Override
            public  void onFailure(Call<Question> call, Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }


}
